# -*- coding: utf-8 -*-

"""StaffLog - Terminal
@version: 0.1
@license: GPL 3.0 (see "license" file)
@author: Jose A. Jimenez <jajimc@gmail.com>, Miguel Centeno <miguelcent@gmail.com>"""

import gtk
import gobject
import threading
from opencv.cv import *
from opencv.highgui import *
import images
import predesys

class DialCheckin:
    def __init__(self, faceImage, host, port, eyesDistance, horLeftEyeNoseDistance, verLeftEyeNoseDistance, noseMouthDistance):
        self.gladeFile = "dialcheckin.glade"
        self.builder = gtk.Builder()
        self.builder.add_from_file(self.gladeFile)
        self.dialCheckin = self.builder.get_object("dialCheckin")
        self.imgCurrent = self.builder.get_object("imgCurrent")
        self.spinStateProcess = self.builder.get_object("spinStateProcess")
        self.labProcessState = self.builder.get_object("labProcessState")
        self.labResult = self.builder.get_object("labResult")
        self.labResultSuccess = self.builder.get_object("labResultSuccess")
        self.labResultUnknown = self.builder.get_object("labResultUnknown")
        self.labError = self.builder.get_object("labError")
        
        self.imgCurrent.set_from_pixbuf(images.ipl_to_pixbuf(faceImage))
        
        self.host = host
        self.port = port
        self.eyesDistance = eyesDistance
        self.horLeftEyeNoseDistance = horLeftEyeNoseDistance
        self.verLeftEyeNoseDistance = verLeftEyeNoseDistance
        self.noseMouthDistance = noseMouthDistance

    def run(self):
        self.dialCheckin.show_all()
        ImageProcessThread(self).start()
        self.dialCheckin.run()
        self.dialCheckin.destroy()
        
class ImageProcessThread(threading.Thread):
    def __init__(self, mainWindow):
        self.mainWindow = mainWindow
        threading.Thread.__init__(self)
    
    def run(self):
        try:
            userData = predesys.checkin(self.mainWindow.host, self.mainWindow.port, self.mainWindow.eyesDistance, self.mainWindow.horLeftEyeNoseDistance, self.mainWindow.verLeftEyeNoseDistance, self.mainWindow.noseMouthDistance)
            
            if userData == "":
                self.mainWindow.labResultUnknown.set_text("Autentificación fallida")
                self.mainWindow.labResultUnknown.set_visible(True)
            else:
                self.mainWindow.labResultSuccess.set_text("Checkin registrado con éxito.\nBienvenido, %s." % userData[1])
                self.mainWindow.labResultSuccess.set_visible(True)
        except:
            self.mainWindow.labResultUnknown.set_text("Se ha producido un error.")
            self.mainWindow.labResultUnknown.set_visible(True)
            
        self.mainWindow.spinStateProcess.set_visible(False)
        self.mainWindow.labProcessState.set_text("Proceso finalizado")
